package com.pattern.creational_pattern.proptype_pattern;

import java.util.Objects;

/**
 * @Description PROPOTYPE PATTERN
 * @Author Ping
 * @Date 2022/3/20 23:45
 **/
public class Book  implements  Cloneable {
    private String name ;
    public Book(String name) {
        this.name = name;
     }

    public String getName() {
        return name;
    }

    @Override
    public Object clone() {
        Book clone = null ;
        try {
            System.out.println("do clone") ;
            clone = (Book) super.clone();
            // clone its attribute to implement deep clone.
        } catch (CloneNotSupportedException e) {
            System.err.println("clone not supported for its super class") ;
////            e.printStackTrace();
        }
        return clone ;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Book)) return false;
        Book book = (Book) o;
        return book.getName().equals(this.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    public static void main(String args[]) {
        Book propotype = new Book("hello") ;
        Book clone = (Book) propotype.clone() ;
//        System.out.println(propotype.getName()) ;
//        System.out.println(clone.getName()) ;
//        System.out.println(propotype.equals(clone));
    }
}
